﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
using CoreMobile.csv;
using ERIMobile.oo;

namespace ERIMobile.ui
{
    public partial class ListForm : Form
    {
        private List<Event> events;

        public ListForm(List<Event> _events)
        {
            InitializeComponent();
            this.events = _events;

            foreach (var _event in _events)
            {
                List.Items.Add(new ListViewItem(new string[]
                                                    {
                                                        _event.Actor.ppRut()
                                                        , _event.Timestamp.ToString("H:mm")
                                                        //, _event.Items.Count.ToString()
                                                        ,_event.Items.Values.Aggregate(0,(a,b) => a + b).ToString()
                                                    }));
            }
        }

        public ListView List
        {
            get { return this.registries; }

        }

        private void export_Click(object sender, EventArgs e)
        {
            List<string[]> data=new List<string[]>();

            foreach (Event evt in events)
            {
                List<string> row = new List<string>();
                row.Add(evt.Code);
                row.Add(evt.Actor.Rut);
                row.Add(evt.Timestamp.ToString("HH:mm dd/MM/yyyy"));
                foreach (var item in evt.Items)
                {
                  row.Add(item.Key);
                  row.Add(item.Value + "");
                }
                data.Add(row.ToArray());
            }

            string status = CSVDocument.exportData(
                new string[]{"Evento","Persona","Fecha","Items-Cantidad ..."}, 
                data ,
                @"\My Documents\eri-registros-"+(DateTime.Now.ToString("yyMMddHHmm"))+".csv");
            if (!string.IsNullOrEmpty(status))
            {
                MessageBox.Show("Error exportar: "+status);
            }
            else
            {
                MessageBox.Show(@"Exportado a \My Documents\");
            }
        }
    }
}